package com.ruoyi.logistics.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.logistics.domain.TraceLngLat;
import com.ruoyi.logistics.service.TraceLngLatService;
import com.ruoyi.logistics.mapper.TraceLngLatMapper;
import com.ruoyi.logistics.utils.hbase.HBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
* @author lx
* @description 针对表【trace_lng_lat】的数据库操作Service实现
* @createDate 2023-05-29 09:50:00
*/
@Service
public class TraceLngLatServiceImpl extends ServiceImpl<TraceLngLatMapper, TraceLngLat>
    implements TraceLngLatService{
    @Autowired
    private HBaseService hBaseService;

    private static final String GPS_PROPERTY = "gps_";
    @Override
    public void saveGpsTable(TraceLngLat traceLngLat) {
        try {
            //先判断表是否存在 不存在则创建表
            if (!hBaseService.tableExists(GPS_PROPERTY+traceLngLat.getCropsId())){
                hBaseService.createTable(GPS_PROPERTY+traceLngLat.getCropsId(),"info","location");
            }
            //添加数据
            String rowKey = traceLngLat.getCropsId() + "_" + System.currentTimeMillis();
            hBaseService.insertOrUpdate(GPS_PROPERTY+traceLngLat.getCropsId(),rowKey,"info","CropsId",traceLngLat.getCropsId());
            hBaseService.insertOrUpdate(GPS_PROPERTY+traceLngLat.getCropsId(),rowKey,"location","lng",String.valueOf(traceLngLat.getLng()));
            hBaseService.insertOrUpdate(GPS_PROPERTY+traceLngLat.getCropsId(),rowKey,"location","lat",String.valueOf(traceLngLat.getLat()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<TraceLngLat> getGpsTable(String cropsId) {
        try {
            //获取表信息
            return hBaseService.getGpsTable(cropsId);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteTable(String cropsId) {
        try {
            //删除对应的表
            hBaseService.deleteTable(GPS_PROPERTY+cropsId);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}




